package com.yxhl.zoume.core.user.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yxhl.zoume.BuildConfig;
import com.yxhl.zoume.R;
import com.yxhl.zoume.account.event.LoginStatus;
import com.yxhl.zoume.account.event.LoginViewBackEvent;
import com.yxhl.zoume.account.event.UserLogoutEvent;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.core.user.presenter.UserCenterPresenter;
import com.yxhl.zoume.core.user.ui.activity.UserContainerActivity;
import com.yxhl.zoume.core.user.view.UserCenterHomeView;
import com.yxhl.zoume.data.http.model.account.ZMAccount;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.usercenter.DaggerUserCenterComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.domain.interactor.ZMEventSubscriber;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.ZMDialog;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;


public class MoreFragment extends BaseFragment implements UserCenterHomeView {

    @BindView(R.id.btn_more_loginout)
    Button mBtnMoreLoginout;
    @BindView(R.id.tv_version)
    TextView mTvVersion;
    private LoginStatus isLogin = LoginStatus.LOGIN;
    @Inject
    public UserCenterPresenter mUserCenterPresenter;


    public static MoreFragment newInstance() {
        MoreFragment fragment = new MoreFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onRxBusEvent();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUserCenterPresenter.attachView(this);
        initVersion();
    }

    private void initVersion() {
        mTvVersion.setText("当前版本号："+BuildConfig.VERSION_NAME);
    }

    private void onRxBusEvent() {
        getSubscription().add(toSubscription(LoginViewBackEvent.class, new Action1<LoginViewBackEvent>() {
            @Override
            public void call(LoginViewBackEvent loginViewBackEvent) {
                isLogin = loginViewBackEvent.getIsLogin();

            }
        }));
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isLogin == LoginStatus.NOT_LOGIN) {
            getActivity().onBackPressed();
        }
        processButtonStatus();
    }

    @Override
    public void onPause() {
        super.onPause();
        mUserCenterPresenter.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mUserCenterPresenter.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUserCenterPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        DaggerUserCenterComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(mActivity))
                .build()
                .inject(this);
    }

    private void processButtonStatus() {
        ZMAccount account = AccountUtils.getInstance(mActivity).getAccount();
        if (account != null) {
            if (!TextUtils.isEmpty(account.getAccessToken()) || !TextUtils.isEmpty(account.getAccountName())  ) {
                mBtnMoreLoginout.setVisibility(View.VISIBLE);
            } else {
                mBtnMoreLoginout.setVisibility(View.GONE);
            }
        }

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_user_center_more;
    }

    @Override
    protected void setupToolBar() {
        ((UserContainerActivity) mActivity).setToolbarTitle("更多");
    }


    @OnClick({R.id.ll_more_version_desc, R.id.ll_more_about_zm, R.id.ll_more_protocol, R.id.btn_more_loginout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_more_version_desc:
                getAppComponent().navigator().navigateToWebPageActivity(mActivity, WebPageEntrance.VERSION_INTROCUCTION);
                break;
            case R.id.ll_more_about_zm:
                getAppComponent().navigator().navigateToWebPageActivity(mActivity, WebPageEntrance.ABOUT_US);
                break;
            case R.id.ll_more_protocol:
                getAppComponent().navigator().navigateToWebPageActivity(mActivity, WebPageEntrance.USER_AGREEMENT);
                break;
            case R.id.btn_more_loginout:
                ZMAccount account = AccountUtils.getInstance(mActivity).getAccount();
                if (account != null) {
                    if (!TextUtils.isEmpty(account.getAccessToken())  && !TextUtils.isEmpty(account.getAccountName()) ) {
                        processLogout();
                    }
                }
                break;
        }
    }


    private void processLogout() {
        ZMDialog dialog = new ZMDialog.Builder(mActivity)
                .setMessage("请确认是否退出账号")
                .setPositiveMessage(getString(R.string.special_car_dialog_confirm))
                .setNegativeMessage(getString(R.string.special_car_dialog_cancel))
                .setPositiveClickEvent(new ZMEventSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        super.onNext(o);
                        mUserCenterPresenter.logout();
                    }
                })
                .setNegativeClickEvent(new ZMEventSubscriber())
                .show();
    }

    @Override
    public void logoutResult() {
        RxBus.getInstance().send(new UserLogoutEvent());
        PromptUtils.show(mActivity, "退出成功", 3);
        mBtnMoreLoginout.setVisibility(View.GONE);
        getActivity().onBackPressed();
    }
}
